package com.poizon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poizon.bo.PageBo;
import com.poizon.dao.MomentCommentDao;
import com.poizon.dao.RecordMomentDao;
import com.poizon.dto.MomentInsertDto;
import com.poizon.dto.MomentUpdateDto;
import com.poizon.entity.MomentComment;
import com.poizon.entity.RecordMoment;
import com.poizon.service.intf.RecordMomentService;
import com.poizon.utils.BeanUtils;
import com.poizon.utils.CheckUtils;
import com.poizon.utils.UploadUtils;
import com.poizon.vo.MomentDetailsVo;
import com.poizon.vo.MomentVo;
import com.poizon.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Set;


/**
 * @ClassName RecordMomentServiceImpl
 * @Author NoCheng
 * @Date 2022/1/3 16:27
 * @Description:TODO 动态业务实现层
 */

@Service
public class RecordMomentServiceImpl implements RecordMomentService {
    private final RecordMomentDao dao;
    private final MomentCommentDao commentDao;
    private final Set<String> brands;

    public RecordMomentServiceImpl(RecordMomentDao dao, MomentCommentDao commentDao) {
        this.dao = dao;
        this.commentDao = commentDao;
        brands = dao.queryBrandName();
    }

    @Override
    public R publish(MomentInsertDto dto) {
        //检查标题和内容
        if (CheckUtils.checkTitle(dto.getTitle()) && CheckUtils.checkMomentContent(dto.getMomentContent())){
            RecordMoment moment = BeanUtils.beanConvert(RecordMoment.class, dto, dto.getClass().getDeclaredFields());
            //上传资源并将返回的访问路径存入对象
            moment.setCover(UploadUtils.fileUpload(dto.getCover()));
            moment.setPicture(UploadUtils.fileUpload(dto.getPicture()));
            System.err.println(moment.getCover());
            System.err.println(moment.getPicture());
            //插入
            dao.insert(moment);
            return R.ok();
        }
        return R.fail("非法请求");

    }

    @Transactional
    @Override
    public R del(Long id) {
        RecordMoment moment = dao.selectById(id);
        //删除已存储的资源
        UploadUtils.deleteBatch(moment.getCover());
        UploadUtils.deleteBatch(moment.getPicture());
        //删除评论数据
        QueryWrapper<MomentComment> wrapper= new QueryWrapper<>();
        wrapper.eq("moment_id", id);
        commentDao.delete(wrapper);
        //删除
        dao.deleteById(id);
        return R.ok();
    }

    @Override
    public R update(MomentUpdateDto dto, MultipartFile cover, MultipartFile... picture) {
        //检查标题和内容
        if (CheckUtils.checkTitle(dto.getTitle()) && CheckUtils.checkMomentContent(dto.getMomentContent())){
            RecordMoment recordMoment = dao.selectById(dto.getId());
            //删除已存储的资源
            UploadUtils.deleteBatch(recordMoment.getCover());
            UploadUtils.deleteBatch(recordMoment.getPicture());
            //类转换
            RecordMoment moment = BeanUtils.beanConvert(RecordMoment.class, dto, dto.getClass().getDeclaredFields());
            //存储资源
            moment.setCover(UploadUtils.fileUpload(cover));
            moment.setPicture(UploadUtils.fileUpload(picture));
            moment.setCommentTime(new Date());
            dao.updateById(moment);
            return R.ok();
        }
        return R.fail("非法请求");
    }

    @Override
    public R selectByType(PageBo bo, Long momentTypeId) {
        QueryWrapper<RecordMoment> wrapper = new QueryWrapper<>();
        wrapper.eq("m.moment_type_id", momentTypeId).orderByAsc("m.comment_time");
        Page<RecordMoment> page = new Page<>(bo.getCurrent(), bo.getSize());
        IPage<MomentVo> momentPage = dao.queryPage(page, wrapper);
        return R.ok(momentPage.getRecords());
    }



    @Override
    public R selectByCondition(PageBo bo, String condition) {
        Page<RecordMoment> page = new Page<>(bo.getCurrent(), bo.getSize());
        QueryWrapper<RecordMoment> wrapper = new QueryWrapper<>();
        if (CheckUtils.isItemCode(condition)){//校验是否为货号
            wrapper.eq("item.item_code", condition).orderByAsc("mom.comment_time");
            IPage<MomentVo> moments = dao.queryPageByItem(page, wrapper);
            return R.ok(moments.getRecords());
        }
        //查询是否为品牌名
        IPage<MomentVo> moments = null;
        if (brands.contains(condition)) {
            //是品牌名
            wrapper.eq("brand.brand_name", condition).orderByAsc("comment_time");
            moments = dao.queryPageByItem(page, wrapper);
        }else {
            //不是品牌名
            wrapper.like("com.title", "%"+condition+"%").or()
                    .like("mom.moment_content", "%"+condition+"%")
                    .orderByAsc("comment_time");
            moments = dao.queryPage(page, wrapper);
        }
        return R.ok(moments.getRecords());
    }

    @Override
    public R selectDetails(Long id){
        MomentDetailsVo detailsVo = dao.queryDetails(id);
        return R.ok(detailsVo);
    }

}
